package main.main;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.*;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class DrawView extends View implements View.OnTouchListener{
	public ArrayList<Brick> Bricks;
	public Paddle thePaddle;
	public Ball theBall;
	private Paint ballPaint, brickPaint;
	private Context myContext;
	public int score=0;
	private boolean started=false;
	public DrawView(Context cxt) {
		super(cxt);
		//myContext=context;
		setFocusableInTouchMode(true);
		this.setOnTouchListener(this);
		Brick.setVars(cxt);
		setupBricks(cxt);
		theBall = new Ball(cxt, -50, -50, 10);
		if (!started) {
			theBall.setX(Brick.WIDTH/2);
			theBall.setY((3*Brick.HEIGHT)/4);
			started=true;
		}
		thePaddle = new Paddle(cxt, Brick.WIDTH/2, Brick.WIDTH/10);
	}
	@Override
	protected void onDraw(Canvas canvas) {
		canvas.drawColor(Color.BLUE);
		if (started) {
			move();
		}
		ballPaint = new Paint();
		ballPaint.setColor(Color.RED);
		ballPaint.setAntiAlias(true);
		brickPaint = new Paint();
		brickPaint.setColor(Color.WHITE);
		brickPaint.setAntiAlias(true);
		canvas.drawCircle(theBall.getX(), theBall.getY(), theBall.getRadius(), ballPaint);
		Rect r;
		for (Brick b : Bricks) {
			//System.out.println(Bricks.size());
			r = new Rect(b.getX(), b.getY(), b.getX()+b.getWidth(), b.getY()+b.getWidth());
			canvas.drawRect(r, brickPaint);
		}
		r = new Rect(thePaddle.getX()-thePaddle.getWidth(), Brick.HEIGHT*99/100, thePaddle.getX()+thePaddle.getWidth(), Brick.HEIGHT);
		canvas.drawRect(r, brickPaint);
		int digits=1;
		if (score>=10) digits++;
		canvas.drawText("Your score is: "+score, 0, 15+digits, 0, 500, brickPaint);
		invalidate();
	}
	public boolean onTouch(View v, MotionEvent event) {
		if (!started) {
			theBall.setX(Brick.WIDTH/2);
			theBall.setY((3*Brick.HEIGHT)/4);
		}
		started=true;
		int px, tx;
		px=thePaddle.getX();
		tx=(int) event.getX();
		if (px<thePaddle.getWidth()) {
			px=thePaddle.getWidth();
		} else if (px>Brick.WIDTH-thePaddle.getWidth()) {
			px=Brick.WIDTH-thePaddle.getWidth();
		}
		if (px-tx>Paddle.MAX_SPEED) {
			thePaddle.setX(thePaddle.getX()-10);
		} else if (tx-px>Paddle.MAX_SPEED){ 
			thePaddle.setX(thePaddle.getX()+10);
		} else {
			thePaddle.setX(tx);
		}
		invalidate();
		return true;
	}
	
	private void setupBricks(Context cxt) {
		Bricks = new ArrayList<Brick>();
		Brick tBrick;
		for (int i=0 ;i<Brick.ROWS; i++) {
			for (int j=0; j<Brick.COLS; j++) {
				tBrick = new Brick(cxt, Brick.MARGIN+(j*Brick.WIDTH)/Brick.COLS, Brick.MARGIN+(i*Brick.WIDTH)/(Brick.COLS), Brick.WIDTH/Brick.ROWS-2*Brick.MARGIN);
				tBrick.setWidth(46);
				Bricks.add(tBrick);
			}
		}
	}
	private void move() {
		boolean collision=false;
		theBall.setX(theBall.getX()+theBall.getXV());
		theBall.setY(theBall.getY()+theBall.getYV());
		for (int i=0; i<Bricks.size(); i++) {
			if (theBall.overlaps(Bricks.get(i))) {
				theBall.bounce(Bricks.get(i));
				Bricks.remove(i);
				score++;
				if (Bricks.size()==0) {
					win();
					return;
				}
				i--;
				collision=true;
				break;
			}
		}
		if (!collision) {
			collision = theBall.checkPaddle(thePaddle);
		}
		if (!collision) {
			if (theBall.onMove()) {
				lose();
			}
		}
	}
	private void win() {
		Toast.makeText(myContext, "YOU WIN!!", Toast.LENGTH_LONG).show();
	}
	private void lose() {
		Toast.makeText(myContext,  "YOU LOSE!! HAHA", Toast.LENGTH_LONG).show();
	}
}
